import { useContext } from 'react';
import { MobXProviderContext } from 'mobx-react';
import { BLOG_INFO, BlogInfo } from './blog';
import { BLOG_CONTENT, BlogContent } from './content';

function createClientStores(initialState: Obj) {
    return {
        [BLOG_INFO]: new BlogInfo(initialState[BLOG_INFO]),
        [BLOG_CONTENT]: new BlogContent(initialState[BLOG_CONTENT]),
    };
}

function createServerStores() {
    return {
        [BLOG_INFO]: new BlogInfo(),
        [BLOG_CONTENT]: new BlogContent(),
    };
}

function getStores(isServer: boolean = false) {
    if (!isServer) {
        return createClientStores(window.__INITIAL_STATE__);
    }
    return createServerStores();
}

function useStores(name: string) {
    return useContext(MobXProviderContext)[name];
}

export {
    getStores,
    useStores,
};
